//等待界面

/**
 * 构造方法 
 */
var Loading = function(_parent, _content) {

    // --------------------------------------------------------------------------
    // import
    // --------------------------------------------------------------------------
	
	//参数检测    
    if (arguments.length < 1) {
        throw new Error('传入参数个数错误,当前个数' + arguments.length + ",需要传入前1个参数。");
    }
	
	var self = this;
	var conShowStyle = "width:100%;height:100%;background-color:#000;display:block;";
    var conHideStyle = "display:none;"
	var con, label;
	
	// --------------------------------------------------------------------------
    //
    // Attributes
    //
    // --------------------------------------------------------------------------
	
    // ------------------------------
    // parent
    // ------------------------------

    /**
     * 父容器
     */
    var parent = _parent;
    this.parent = function(value) {
        if (!arguments.length) {
            return parent;                
        }
        parent = value;
        return this;
    };
    
    // ------------------------------
    // visible
    // ------------------------------

    /**
     * 是否可见
     */
    var visible = true;
    this.visible = function(value) {
        if (!arguments.length) {
            return visible;                
        }
        visible = value;
        update();
        return this;
    };
    
    // ------------------------------
    // content
    // ------------------------------

    /**
     * 文本内容
     */
    var content = _content;
    this.content = function(value) {
        if (!arguments.length) {
            return content;                
        }
        content = value;
        update();
        return this;
    };
    
    // --------------------------------------------------------------------------
    //
    // private methods
    //
    // --------------------------------------------------------------------------
    
    var init = function() {
        //父容器检测
        if (typeof(_parent) == 'string') {
            _parent = document.getElementById(_parent);
        }
        if (_parent) {
            self.parent(_parent);
        } else {
            throw new Error('_parent构造方法中传入的容器_parent:' + _parent + "无法找到。");
        }
        
        //文本内容检测
        if (typeof(_content) != 'string') {
            _content = "";
            self.content(_content);
        }
        
        //创建内容
        create();
    };
    
    var getLableText = function() {
    	return self.content() ? self.content() : "请等待...";
    };
    
    var create = function() {
    	con = document.createElement("div");
    	con.setAttribute("style", conShowStyle);
    	
    	label = document.createElement("div");
    	label.setAttribute("class", "loading");
    	var labelStyle = "height:22px;" +
    			"font-size:16px;" +
    			"font-family:Microsoft YaHei;" +
    			"font-weight: bold;" +
    			"color:#000;" +
    			"border:#000 1px solid;" +
    			"background-color:#fff;" +
    			"padding-left: 4px;" +
    			"padding-right: 4px;" +
    			"pointer-events:none;" +
    			//长度适应文本
    			"width:fit-content;" +
    			"width:-webkit-fit-content;" +
    			"width:-moz-fit-content;" +
    			//居中
    			"position:fixed;" +
    			"left:50%;" +
    			"top:50%;" +
    			"margin-left:width/2;" +
    			"margin-top:height/2;";
    			
    	label.setAttribute("style", labelStyle);
    	label.innerHTML = getLableText();
    	
    	con.appendChild(label);
    	self.parent().appendChild(con);
    };
    
    var update = function() {
    	if(label) {
    		label.innerHTML = getLableText();
    	}
    	if(con) {
    		con.setAttribute("style", self.visible() === true ? conShowStyle : conHideStyle);
    	}
    };
	
    //run function
    init();
};

Loading.struct = function(room2d) {
    Loading.prototype.constructor = Loading;
};

module.exports = Loading;